using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace BookShareWebApp
{
    public class UploadController : Controller
    {

        private readonly BookContext _bookcontext;
        private readonly IWebHostEnvironment _hostingEnvironment;
        public UploadController(BookContext bookcontext,IWebHostEnvironment hostingEnvironment)
        {
            _bookcontext = bookcontext;
            _hostingEnvironment = hostingEnvironment;
        }
       


//         public ActionResult Index()
//             {
//                 //Web根目录
//                 string webRootPath = _hostingEnvironment.WebRootPath;
// 　　　　　　　　　 //内容根目录
//                 string contentRootPath = _hostingEnvironment.ContentRootPath;

//                 return Content(webRootPath + "\n" + contentRootPath);
//             }
        [HttpPost("/api/upload")]
         
        // public IActionResult Upload(IFormCollection file1)
        // {
        //     var inputs = file1.Files;
        //     var input = inputs[0];
        //     Console.Write("hello");
        //     if (input == null) return BadRequest();

        //     string uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "file");
        
        //     string uniqueFileName = Guid.NewGuid().ToString() + "_" + input.FileName;

        //     string filePath = Path.Combine(uploadsFolder,uniqueFileName);

        //     input.CopyTo(new FileStream(filePath, FileMode.Create));

        //     return Ok();
            
        // }
        public JsonResult uploadProject(IFormCollection file1)
        {
            var files = file1.Files;
            var file = files[0];
            if (file != null)
            {
                string fileDir = Path.Combine(Directory.GetCurrentDirectory(), "file");
                
                if (!Directory.Exists(fileDir))
                {
                    Directory.CreateDirectory(fileDir);
                }
                //文件名称
                string projectFileName = file.FileName;

                //上传的文件的路径
                string filePath = fileDir + $@"\{projectFileName}";
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                return Json("ok");
            }else{
                return Json("no");
            }
        }
        
        [HttpGet("/api/download/{filename}")]

        // public FileStream Download(string filename){
        //     string uploadsFolder;
        //     if (string.IsNullOrWhiteSpace(_hostingEnvironment.WebRootPath))
        // {
        //     uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "file");
        // }
        // else{
        //     uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath,"file");
        // }
            
            
        //     string filePath = Path.Combine(uploadsFolder,filename);

        //     FileStream file = new FileStream(filePath,FileMode.Open);
                
        //     return file;
            
        // }
         public async Task<IActionResult> DownloadFile(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return NotFound();
            }
            var memoryStream = new MemoryStream();
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                await stream.CopyToAsync(memoryStream);
            }
            memoryStream.Seek(0, SeekOrigin.Begin);
            string[] strArry = filename.Split('/');
            string fileName = strArry[strArry.Length - 1];
            //文件名必须编码，否则会有特殊字符(如中文)无法在此下载。
            string encodeFilename = System.Net.WebUtility.UrlEncode(fileName);
            Response.Headers.Add("Content-Disposition", "attachment; filename=" + encodeFilename);
            return new FileStreamResult(memoryStream, "application/pdf");//文件流方式，指定文件流对应的ContenType。
        }
        //下载的pdf为空
       
       
        
    }
}